package com.zw.dal.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常捕捉
 * Created by zwz
 * date 2016/11/21
 * project com.zw.dal.exception
 */
@ControllerAdvice(basePackages = "com.zw.dal")
public class GlobalDefaultExceptionHandler {

    public static final Logger log = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);


    /**
     * 验证异常捕捉
     * Validation handler string.
     *
     * @param req the req
     * @param e   the e
     * @return the string
     */
    @ExceptionHandler(value = BindException.class)
    public String validationHandler(HttpServletRequest req, BindException e) {
        log.warn(e.getMessage());
        //// TODO: 2016/11/21
        /*
            如果是验证相关的异常信息 则反回给请求页面
            而不是抛出

         */
        return "";
    }

    /**
     * 父类异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public String defaultErrorHandler(HttpServletRequest req, Exception e) {
        e.printStackTrace();
        System.out.println("GlobalDefaultExceptionHandler.defaultErrorHandler()");
        return e.getMessage();
    }
}